package com.tanhua.server.service;
import com.tanhua.domain.vo.VoiceVo;
import org.bson.types.ObjectId;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Voice;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.VoiceApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * wzp 2021/6/23 10:45
 */
@Service
public class VoiceService {

    @Reference
    private VoiceApi voiceApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private FastFileStorageClient storageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;

    public ResponseEntity<Object> sendVoice(MultipartFile soundFile) throws IOException {
        //获得语音文件名称
        String voiceFileName=soundFile.getOriginalFilename();
        //获得语音文件后缀名
        String ext=voiceFileName.substring(voiceFileName.lastIndexOf(".")+1);
        //存入fdfs中，并获得存储路径
        StorePath storePath = storageClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(), ext, null);
        String soundUrl=fdfsWebServer.getWebServerUrl()+storePath.getFullPath();
        //封装数据
        UserInfo userInfo = userInfoApi.findById(UserHolder.getUserId());
        Voice voice = new Voice();
        voice.setId(new ObjectId());
        voice.setVid(0L);
        voice.setUserId(userInfo.getId());
        voice.setGender(userInfo.getGender());
        voice.setAge(userInfo.getAge());
        voice.setSoundUrl(soundUrl);
        voice.setCreated(System.currentTimeMillis());
        voice.setState(1);
        //存入mongo数据库中
        voiceApi.save(voice);
        return ResponseEntity.ok(null);
    }

    /**
     * 接口名称：桃花传音-接收语音（学生实战）
     * 接口路径：GET/peachblossom
     */
    public ResponseEntity<Object> getVoice() {
        Long userId = UserHolder.getUserId();
        //获得用户接受语音剩余次数
        Integer time = voiceApi.timesSubtract(userId);
        VoiceVo voiceVo = new VoiceVo();
        if (time!=0){
            //获得语音封装结果
            Voice voice = voiceApi.findVoice(userId);
            //获得语音对象用户的用户信息
            UserInfo userInfo = userInfoApi.findById(voice.getUserId());
            if (userInfo!=null && voice!=null) {
                BeanUtils.copyProperties(userInfo, voiceVo);
                //语音对象id
                voiceVo.setId(userInfo.getId().intValue());
                voiceVo.setSoundUrl(voice.getSoundUrl());
            }else {
                //语音为空时返回提示
                voiceVo.setNickname("抱歉，没有漂流的语音了");
                voiceVo.setAge(0);
            }
            voiceVo.setRemainingTimes(time);
        }else {
            //次数为空时返回提示
            voiceVo.setRemainingTimes(time);
            voiceVo.setAge(0);
        }
        return ResponseEntity.ok(voiceVo);
    }
}
